import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

//import org.eclipse.jface.dialogs.MessageDialog;

public class V extends JFrame {
 private static final long serialVersionUID = 8665272220673200148L;
 
 private JPanel pnlMain = new JPanel();
 private JTextField txfInputText = new JTextField();
 //private JTextArea txaOutputText = new JTextArea();
 public JTextArea txaOutputText = new JTextArea();
 private JButton btnStart = new JButton("Start");
 private JScrollPane jScrollPane = new JScrollPane(txaOutputText);
 
 public V(String title) {
  setSize(700, 400); //                    
  setVisible(true); //            JFrame
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //                 
              //                 
  setTitle(title);
  
  txfInputText.setBounds(25, 25, 500, 25);
  btnStart.setBounds(550, 25, 100, 25);
  jScrollPane.setBounds(25, 75, 625, 250);

  pnlMain.add(txfInputText);
  pnlMain.add(btnStart);
  pnlMain.add(jScrollPane);

  pnlMain.setBackground(Color.WHITE);
  pnlMain.setLayout(null);
  add(pnlMain);

  
  
  
  btnStart.addActionListener(new ActionListener() { //                 
   public void actionPerformed(ActionEvent arg0) {
	   String c =txfInputText.getText();
	   c.trim();
	   String[] c1=c.split(" ");
	   int[] vvid=new int[100];
	   
	   
	   int i=0;
		for(String token:c1){
			vvid[i]=Integer.parseInt(token);
			i++;}
		int[] mass=new int[i];
		for(int r=0;r<i;r++)
		{
			mass[r]=vvid[r];
		}
		     print(mass);
	         sort(mass, 0, i-1);
	         
  }
 });
 }
 
 public void sort(int[] arr, int lo, int hi) {
	 
     if (lo < hi) {
         int mid = (lo + hi) / 2;
         sort(arr, lo, mid);           // recursive call to divide the sub-list
         sort(arr, mid + 1, hi);       // recursive call to divide the sub-list
         merge(arr, lo, mid, hi);      // merge the sorted sub-lists.
         print(arr);
     }
 }

 private void merge(int[] arr, int lo, int mid, int hi) {
     // allocate enough space so that the extra 'sentinel' value
     // can be added. Each of the 'left' and 'right' sub-lists are pre-sorted.
     // This function only merges them into a sorted list.
     int[] left = new int[(mid - lo) + 2];        
     int[] right = new int[hi - mid + 1];         


     // create the left and right sub-list for merging into original list.
     System.arraycopy(arr, lo, left, 0, left.length - 1);
     System.arraycopy(arr, mid + 1, right, 0, left.length - 1);

     // giving a sentinal value to marking the end of the sub-list.
     // Note: The list to be sorted is assumed to contain numbers less than 100.
     left[left.length - 1] = 100;
     right[right.length - 1] = 100;

     int i = 0;
     int j = 0;

     // loop to merge the sorted sequence from the 2 sub-lists(left and right) 
     // into the main list.
     for (; lo <= hi; lo++) {
         if (left[i] <= right[j]) {
             arr[lo] = left[i];
             i++;
         } else {
             arr[lo] = right[j];
             j++;
         }
     }
 }

 public  void print(int[] arr) {
	 txaOutputText.append("\n");
     for (int i : arr) {
    	 txaOutputText.append(i + ", ");
     }
     
 }

public static void main(String[] args) {
 new V("");
}
}
